package terra.util;

import java.io.Serializable;

public class Point2D implements Serializable {

	private static final long serialVersionUID = 1L;

	private volatile int _x, _y;

	public Point2D(int pX, int pY) {
		_x = pX;
		_y = pY;
	}

	public Point2D(Point2D pos) {
		synchronized (pos) {
			_x = pos._x;
			_y = pos._y;
		}
	}

	public synchronized void setTo(Point2D point) {
		synchronized (point) {
			_x = point._x;
			_y = point._y;
		}
	}

	@Override
	public String toString() {
		return "(" + _x + ", " + _y + ")";
	}

	@Override
	public int hashCode() {
		return _x ^ _y;
	}

	@Override
	public synchronized boolean equals(Object o) {
		if (o instanceof Point2D) {
			Point2D point2D = (Point2D) o;
			boolean ret;
			synchronized (point2D) {
				ret = point2D._x == _x && point2D._y == _y;
			}
			return ret;
		}
		return false;
	}

	public synchronized boolean equals(int pX, int pY, int pZ) {
		return _x == pX && _y == pY;
	}

	public synchronized long distanceSquaredTo(Point2D point) {
		long dx, dy;
		synchronized (point) {
			dx = _x - point._x;
			dy = _y - point._y;
		}
		return (dx * dx) + (dy * dy);
	}

	public static long distanceSquared(Point2D point1, Point2D point2) {
		long dx, dy;
		synchronized (point1) {
			synchronized (point2) {
				dx = point1._x - point2._x;
				dy = point1._y - point2._y;
			}
		}
		return (dx * dx) + (dy * dy);
	}

	public static boolean distanceLessThan(Point2D point1, Point2D point2,
			double distance) {
		return distanceSquared(point1, point2) < distance * distance;
	}

	public int getX() {
		return _x;
	}

	public synchronized void setX(int pX) {
		_x = pX;
	}

	public int getY() {
		return _y;
	}

	public synchronized void setY(int pY) {
		_y = pY;
	}

	public synchronized void setXY(int pX, int pY) {
		_x = pX;
		_y = pY;
	}
}
